package com.ruoyi.web.controller.purchase;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.purchase.domain.PoBrand;
import com.ruoyi.purchase.service.PoBrandService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 品牌管理 Controller
 */
@Controller
@RequestMapping("/brand/manage")
public class BrandManageController extends BaseController {
    private String prefix="brand/manage/";

    @Autowired
    private PoBrandService poBrandService;

    @RequiresPermissions("brand:manage:view")
    @RequestMapping()
    public String braManage(){
        return prefix+"braManage";
    }

    @RequiresPermissions("brand:manage:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(PoBrand poBrand){
        startPage();
        List<PoBrand> poBraManageList= poBrandService.findBraManageList(poBrand);
        System.out.println(poBraManageList);
        return getDataTable(poBraManageList);
    }

    @RequestMapping("/add")
    public String add(Model model){
        model.addAttribute("cUser",getSysUser());
        return prefix+"add";
    }

    @RequiresPermissions("brand:manage:add")
    @Log(title = "品牌信息",businessType = BusinessType.INSERT)
    @RequestMapping("/doAdd")
    @ResponseBody
    public AjaxResult addSave(PoBrand poBrand){
        return toAjax(poBrandService.save(poBrand));
    }

    @RequestMapping("/edit/{brandId}")
    public String edit(@PathVariable("brandId") Integer brandId,Model model){
        PoBrand poBrand = poBrandService.getById(brandId);
        model.addAttribute("poBrand",poBrand);
        model.addAttribute("cUser",getSysUser());
        return prefix+"/edit";
    }

    @Log(title = "品牌修改", businessType = BusinessType.UPDATE)
    @RequestMapping("/doEdit")
    @ResponseBody
    public AjaxResult editSave(@Validated PoBrand poBrand){
        System.out.println(poBrand);
        return toAjax(poBrandService.updateById(poBrand));
    }

    @RequiresPermissions("brand:manage:remove")
    @Log(title = "品牌删除", businessType = BusinessType.DELETE)
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Integer ids){
        return toAjax(poBrandService.removeById(ids));
    }





}
